package com.newtype.meeting.client.widget.backend;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Department;

public class DepartmentAddWindow extends Window {

	public DepartmentAddWindow() {
		setHeading("新增部门");
		setModal(true);
		setWidth(400);
		setAutoHeight(true);
		setButtonAlign(HorizontalAlignment.CENTER);

		FormPanel form = new FormPanel();
		form.setFrame(true);
		form.setHeaderVisible(false);

		final HiddenField<String> pDeptID = new HiddenField<String>();
		pDeptID.setFieldLabel("上层部门ID");
		form.add(pDeptID);

		final TextField<String> pDeptName = new TextField<String>();
		pDeptName.setFieldLabel("上层部门");
		pDeptName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> pDept = new HashMap<String, Field<String>>();
				pDept.put("pDeptID", pDeptID);
				pDept.put("pDeptName", pDeptName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeSingleWindow, pDept);
			}
		});
		form.add(pDeptName);

		final TextField<String> deptName = new TextField<String>();
		deptName.setFieldLabel("部门名称");
		deptName.setAllowBlank(false);
		deptName.getMessages().setBlankText("栏位不可为空白");
		form.add(deptName);

		final CheckBox checkExpand = new CheckBox();
		checkExpand.setFieldLabel("是否展开");
		checkExpand.setBoxLabel("树状结构显示时展开");
		form.add(checkExpand);

		Radio radio1 = new Radio();
		radio1.setBoxLabel("启用");
		radio1.setValueAttribute("1");
		radio1.setValue(true);

		Radio radio2 = new Radio();
		radio2.setBoxLabel("停用");
		radio2.setValueAttribute("0");

		final RadioGroup deptStatus = new RadioGroup();
		deptStatus.setFieldLabel("部门状态");
		deptStatus.add(radio1);
		deptStatus.add(radio2);
		form.add(deptStatus);

		Button btnSend = new Button("送出");
		btnSend.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Department department = new Department();
				department.setParentDeptID(pDeptID.getValue());
				department.setDeptName(deptName.getValue());
				Integer isExpand = 0;
				if (checkExpand.getValue()) {
					isExpand = 1;
				}
				department.setIsExpand(isExpand);
				Integer isEnabled = Integer.parseInt(deptStatus.getValue()
						.getValueAttribute());
				department.setIsEnabled(isEnabled);

				Dispatcher.forwardEvent(AppEvents.SaveDepartment, department);
				hide();
			}
		});
		addButton(btnSend);

		FormButtonBinding binding = new FormButtonBinding(form);
		binding.addButton(btnSend);
		add(form);
	}
}
